package com.easylinkin.linkappapi.common.config;

import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>swagger 接口文档</p>
 *
 * @author TongJie
 * @since 2020/6/12 18:14
 */
@Configuration
//@Profile({"dev", "test"})
// @EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
            .apiInfo(apiInfo())
            .select()
            //为当前包路径
//            .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
            .apis(RequestHandlerSelectors.basePackage("com.easylinkin.linkappapi"))
            .paths(PathSelectors.any())
            .build().groupName("全量接口")
            .securitySchemes(security());
//        return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)).build();
    }

    private List<ApiKey> security() {
        ApiKey apikey = new ApiKey("token", "token", "header");
        ArrayList list = new ArrayList();
        list.add(apikey);
        return list;
    }

    //构建 api文档的详细信息函数,注意这里的注解引用的是哪个
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
            //页面标题
            .title("Linkapp 接口文档")
            //创建人
            .contact(new Contact("JasonTong", "http://blog.bianxh.top/", "tj20111231@163.com"))
            //版本号
            .version("1.0")
            //描述
            .description("API 描述")
            .build();
    }

    /*  *************************   二级分组区   start   ************************* */

    @Bean
    public Docket openApiGroup() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(new ApiInfoBuilder()
                        .title("OPEN API")
                        .contact(new Contact("QiHang Liu", "太穷，没得博客", "302728910@qq.com"))
                        .description("dataV、 数据推送、开放接口调用、同步等相关接口")
                        .version("1.0.1")
                        .build())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.easylinkin.linkappapi.openapi.controller"))
                .build()
                .groupName("OpenApi 接口")
                .pathMapping("/");
    }

    @Bean
    public Docket deviceGroup() {
        Predicate<RequestHandler> deviceHandler = RequestHandlerSelectors.basePackage("com.easylinkin.linkappapi.device.controller");
        Predicate<RequestHandler> deviceattributestatusHandler = RequestHandlerSelectors.basePackage("com.easylinkin.linkappapi.deviceattributestatus.controller");
        Predicate<RequestHandler> deviceparmHandler = RequestHandlerSelectors.basePackage("com.easylinkin.linkappapi.deviceparm.controller");
        Predicate<RequestHandler> deviceserviceHandler = RequestHandlerSelectors.basePackage("com.easylinkin.linkappapi.deviceservice.controller");
        Predicate<RequestHandler> devicetypeHandler = RequestHandlerSelectors.basePackage("com.easylinkin.linkappapi.devicetype.controller");
        Predicate<RequestHandler> deviceunitHandler = RequestHandlerSelectors.basePackage("com.easylinkin.linkappapi.deviceunit.controller");

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(new ApiInfoBuilder()
                        .title("设备(device) API")
                        .description("设备数据操作查询等相关接口")
                        .version("1.0.1")
                        .build())
                .select()
                .apis(Predicates.or(deviceHandler,deviceattributestatusHandler, deviceparmHandler, deviceserviceHandler, devicetypeHandler, deviceunitHandler))
//                .apis(deviceHandler)
                .build()
                .groupName("设备(device)接口")
                .pathMapping("/");
    }

    @Bean
    public Docket spaceGroup() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(new ApiInfoBuilder()
                        .title("空间区域(space) API")
                        .description("空间区域增删改查操作相关接口")
                        .version("1.0.1")
                        .build())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.easylinkin.linkappapi.space.controller"))
                .build()
                .groupName("空间区域(space)接口")
                .pathMapping("/");
    }

    @Bean
    public Docket tenantGroup() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(new ApiInfoBuilder()
                        .title("租户相关(tenant) API")
                        .description("租户、租户设备型号相关接口")
                        .version("1.0.1")
                        .build())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.easylinkin.linkappapi.tenant.controller"))
                .build()
                .groupName("租户相关(tenant)接口")
                .pathMapping("/");
    }

    @Bean
    public Docket elasticSearchGroup() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(new ApiInfoBuilder()
                        .title("ES查询(elasticSearch) API")
                        .description("ES查询相关接口")
                        .version("1.0.1")
                        .build())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.easylinkin.linkappapi.elasticsearch.controller"))
                .build()
                .groupName("ES查询(elasticSearch)接口")
                .pathMapping("/");
    }

    @Bean
    public Docket alarmGroup() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(new ApiInfoBuilder()
                        .title("告警(alarm) API")
                        .description("告警相关接口")
                        .version("1.0.1")
                        .build())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.easylinkin.linkappapi.alarm.controller"))
                .build()
                .groupName("告警(alarm)接口")
                .pathMapping("/");
    }

    @Bean
    public Docket ruleEngineGroup() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(new ApiInfoBuilder()
                        .title("规则引擎(ruleEngine) API")
                        .description("规则引擎接口")
                        .version("1.0.1")
                        .build())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.easylinkin.linkappapi.ruleengine.controller"))
                .build()
                .groupName("规则引擎(ruleEngine)接口")
                .pathMapping("/");
    }

    @Bean
    public Docket securityGroup() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(new ApiInfoBuilder()
                        .title("安全(security) API")
                        .description("规则引擎接口")
                        .version("1.0.1")
                        .build())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.easylinkin.linkappapi.security.controller"))
                .build()
                .groupName("安全(security)接口")
                .pathMapping("/");
    }

    @Bean
    public Docket dataSourceGroup() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(new ApiInfoBuilder()
                        .title("数据源API")
                        .description("数据源接口")
                        .version("1.0.1")
                        .build())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.easylinkin.linkappapi.analyze.controller"))
                .build()
                .groupName("数据源接口")
                .pathMapping("/");
    }

    @Bean
    public Docket knowledgeBaseGroup() {
        return new Docket(DocumentationType.SWAGGER_2)
            .apiInfo(new ApiInfoBuilder()
                .title("知识库(knowledgebase) API")
                .description("知识库相关接口")
                .version("1.0.1")
                .build())
            .select()
            .apis(RequestHandlerSelectors.basePackage("com.easylinkin.linkappapi.knowledgebase.controller"))
            .build()
            .groupName("知识库(knowledgebase) 接口")
            .pathMapping("/");
    }

    @Bean
    public Docket inspectionGroup() {
        return new Docket(DocumentationType.SWAGGER_2)
            .apiInfo(new ApiInfoBuilder()
                .title("配电箱(inspection) API")
                .description("配电箱相关接口")
                .version("1.0.1")
                .build())
            .select()
            .apis(RequestHandlerSelectors.basePackage("com.easylinkin.linkappapi.inspection.controller"))
            .build()
            .groupName("配电箱(inspection) 接口")
            .pathMapping("/");
    }
    /*  *************************   二级分组区   end   ************************* */
}
